<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\RoomController;
use App\Http\Controllers\Api\OrderController;
use App\Http\Controllers\Api\DishController;
use App\Http\Controllers\Api\PerformanceController;
use App\Http\Controllers\Api\CategoryController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

// 用户认证路由
Route::prefix('auth')->group(function () {
    Route::post('login', [AuthController::class, 'login']);
    Route::middleware('auth:sanctum')->group(function () {
        Route::get('me', [AuthController::class, 'me']);
        Route::post('refresh', [AuthController::class, 'refresh']);
        Route::post('logout', [AuthController::class, 'logout']);
    });
});

// 需要认证的路由
Route::middleware('auth:sanctum')->group(function () {
    // 房间管理
    Route::prefix('rooms')->group(function () {
        Route::post('/', [RoomController::class, 'store']);
        
        // 房间列表相关接口（必须在参数路由之前）
        Route::get('my', [RoomController::class, 'myRooms']);
        Route::get('recent', [RoomController::class, 'recentRooms']);
        Route::get('pinned', [RoomController::class, 'pinnedRooms']);
        
        // 参数路由（必须在具体路由之后）
        Route::post('{roomId}/join', [RoomController::class, 'join']);
        Route::get('{roomId}', [RoomController::class, 'show']);
        Route::get('{roomId}/menu', [RoomController::class, 'getMenu']);
        Route::put('{roomId}/settings', [RoomController::class, 'updateSettings']);
        Route::delete('{roomId}', [RoomController::class, 'destroy']);
        Route::post('{roomId}/pin', [RoomController::class, 'pinRoom']);
        Route::delete('{roomId}/pin', [RoomController::class, 'unpinRoom']);
    });

    // 菜品管理
    Route::prefix('rooms/{roomId}/dishes')->group(function () {
        Route::get('/', [DishController::class, 'getRoomDishes']);
        Route::get('search', [DishController::class, 'searchRoomDishes']);
        Route::post('/', [DishController::class, 'addCustomDish']);
        Route::put('{dishId}', [DishController::class, 'updateDish']);
        Route::delete('{dishId}', [DishController::class, 'deleteDish']);
        Route::post('{dishId}/copy', [DishController::class, 'copyDishToRoom']);
    });

    // 用户自定义菜品
    Route::prefix('user/dishes')->group(function () {
        Route::get('/', [DishController::class, 'getUserCustomDishes']);
        Route::post('/', [DishController::class, 'createUserCustomDish']);
    });

    // 订单管理
    Route::prefix('orders')->group(function () {
        Route::post('/', [OrderController::class, 'store']);
        Route::get('my', [OrderController::class, 'myOrders']);
        Route::get('{orderId}', [OrderController::class, 'show']);
        Route::delete('{orderId}', [OrderController::class, 'destroy']);
        Route::post('{orderId}/confirm', [OrderController::class, 'confirm']);
    });

    // 房间订单
    Route::get('rooms/{roomId}/orders', [OrderController::class, 'getRoomOrders']);
});

// 性能监控路由（需要认证）
Route::middleware('auth:sanctum')->group(function () {
    Route::prefix('performance')->group(function () {
        Route::get('system', [PerformanceController::class, 'systemMetrics']);
        Route::get('database', [PerformanceController::class, 'databaseMetrics']);
        Route::get('cache', [PerformanceController::class, 'cacheMetrics']);
        Route::get('cache-info', [PerformanceController::class, 'cacheInfo']);
        Route::post('clear-cache', [PerformanceController::class, 'clearCache']);
        Route::get('log-stats', [PerformanceController::class, 'logStats']);
        Route::post('benchmark', [PerformanceController::class, 'benchmark']);
        Route::get('thresholds', [PerformanceController::class, 'checkThresholds']);
        Route::get('activity-logs', [PerformanceController::class, 'activityLogs']);
    });
});

// 分类管理路由（无需认证）
Route::prefix('categories')->group(function () {
    Route::get('tree', [CategoryController::class, 'tree']);
    Route::get('main', [CategoryController::class, 'main']);
    Route::get('sub', [CategoryController::class, 'sub']);
    Route::get('search', [CategoryController::class, 'search']);
    Route::get('stats', [CategoryController::class, 'stats']);
    Route::get('{category}', [CategoryController::class, 'show']);
    Route::get('{category}/dishes', [CategoryController::class, 'dishes']);
});
